package com.wish.finance.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wish.finance.entity.CwReceiptPlan;
import com.wish.finance.entity.vo.CwReceiptPlanVo;
import com.wish.finance.mapper.CwReceiptPlanMapper;
import com.wish.finance.service.ICwReceiptPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 收款计划表 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-03-30
 */
@Service
@Primary
public class CwReceiptPlanServiceImpl extends ServiceImpl<CwReceiptPlanMapper, CwReceiptPlan> implements ICwReceiptPlanService {

    @Override
    public IPage<CwReceiptPlan> listShow(CwReceiptPlanVo cwReceiptPlanVo) {
        Page<CwReceiptPlan> page = new Page<>(cwReceiptPlanVo.getCurrent(), cwReceiptPlanVo.getSize());
        return getBaseMapper().listShow(page,cwReceiptPlanVo);
    }

    @Override
    public CwReceiptPlanVo sumList(CwReceiptPlanVo cwReceiptPlanVo) {
        List<CwReceiptPlanVo> list = getBaseMapper().sumList(cwReceiptPlanVo);
        //应收金额
        BigDecimal amountReceivable = BigDecimal.ZERO;
        //实收金额
        BigDecimal amountCollected = BigDecimal.ZERO;

        for (CwReceiptPlanVo receiptPlanVo : list) {
            if (receiptPlanVo.getPaymentAmount() != null) {
                amountReceivable = amountReceivable.add(receiptPlanVo.getPaymentAmount());
//				System.out.println("应收金额："+amountReceivable);
            }
            if (receiptPlanVo.getSkStatus() != null && receiptPlanVo.getSkStatus() == 2) {
                if (receiptPlanVo.getPaymentAmount() != null) {
                    amountCollected = amountCollected.add(receiptPlanVo.getPaymentAmount());
//					System.out.println("实收金额："+amountCollected);
                }
            }
        }
        System.out.println("应收金额共计："+amountReceivable);
        System.out.println("实收金额共计："+amountCollected);

        cwReceiptPlanVo.setCollectionSum(amountReceivable);
        cwReceiptPlanVo.setRealCollectionSum(amountCollected);
        return cwReceiptPlanVo;
    }
}
